import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/login/login'
import Index from '@/views/home'

import Forget from '@/views/login/forget'
import layoutindexAside from '@/layout/'
import layoutindex from '@/layout/indexTop'
import insureList from '@/views/insure/insureList'
import AddInsure from '@/views/insure/addInsure'
import ViewInsure from '@/views/insure/viewInsure'
import EditPolicy from '@/views/insure/edit'
import CaseList from '@/views/case/caseList'
import AddCase from '@/views/case/addCase'
import EditCase from '@/views/case/editCase'
import ViewCase from '@/views/case/viewCase'
import EditInsurance from '@/views/case/editInsurance'
import ChangeCase from '@/views/case/changeCase'

import AddPayBack from '@/views/payBack/addPayBack'
import ViewPayBack from '@/views/payBack/viewPayBack'
import PayBackList from '@/views/payBack/payBackList'
import EditPayBack from '@/views/payBack/editPayBack'

import FundList from '@/views/fund/fundList'
import AddFund from '@/views/fund/addFund'
import ViewFund from '@/views/fund/viewFund'
import EditFund from '@/views/fund/editFund'
import EditFundInsurance from '@/views/fund/editFundInsurance'
import ChangeFund from '@/views/fund/changeFund'

import AddFundPayBack from '@/views/fundPayBack/addFundPayBack'
import ViewFundPayBack from '@/views/fundPayBack/viewFundPayBack'
import FundPayBackList from '@/views/fundPayBack/fundPayBackList'
import EditFundPayBack from '@/views/fundPayBack/editFundPayBack'

import AddResidual from '@/views/residual/addResidual'
import EditResidual from '@/views/residual/editResidual'
import ResidualList from '@/views/residual/residualList'

import AddCompany from '@/views/company/addCompany'
import EditCompany from '@/views/company/editCompany'
import ViewCompany from '@/views/company/viewCompany'
import companyList from '@/views/company/companyList'

import AddBusiness from '@/views/business/addBusiness'
import EditBusiness from '@/views/business/editBusiness'
import ViewBusiness from '@/views/business/viewBusiness'
import BusinessList from '@/views/business/businessList'
import ViewPrice from '@/views/business/viewPrice'
import AddPrice from '@/views/business/addPrice'

import UserList from '@/views/user/userList'
import AddUser from '@/views/user/addUser'
import EditUser from '@/views/user/editUser'
import ViewUser from '@/views/user/viewUser'
import UserRole from '@/views/user/userRole'

import RoleList from '@/views/role/roleList'
import EditRole from '@/views/role/editRole'

import CheckList from '@/views/check/checkList'

import Ucenter from '@/views/ucenter'

import notFond from '@/views/error/404'
import noService from '@/views/error/505'
import noPermission from '@/views/error/noPermission'

import CollectionList from '@/views/collection/collectionList'
import CollectionView from '@/views/collection/collectionView'

import ReportSo from '@/views/report/reportSo'
import ReportPe from '@/views/report/reportPe'
import ReportAc from '@/views/report/reportAc'
import ReportCo from '@/views/report/reportCo'
import ReportPa from '@/views/report/reportPa'

import ManagePe from '@/views/manage/managePe'
import ManageAc from '@/views/manage/manageAc'
import ManageSo from '@/views/manage/manageSo'
import ManageHs from '@/views/manage/manageHs'

import Manage from '@/views/xitong/manage'
import Hand from '@/views/xitong/hand'

Vue.use(Router)
const router = new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/forget',
      name: 'forget',
      component: Forget
    },
    {
      path: '/404',
      name: '404',
      component: notFond
    },
    {
      path: '/505',
      name: '505',
      component: noService
    },
    {
      path: '/',
      redirect: { name: 'index' },
      component: layoutindex,
      children: [
        // 首页
        {
          path: '/index',
          name: 'index',
          component: Index
        },
        // 账号设置
        {
          path: '/ucenter',
          name: 'ucenter',
          component: Ucenter
        },
      ]
    },
    {
      path: '/',
      redirect: { name: 'index' },
      component: layoutindexAside,
      children: [
        // 无权限
        {
          path: '/noPermission',
          name: 'noPermission',
          component: noPermission
        },
        // 参保地
        {
          path: 'policy/list',
          name: 'addrlist',
          component: insureList
        },
        {
          path: 'policy/addInsure',
          name: 'addr4add',
          component: AddInsure
        },
        {
          path: 'policy/view',
          name: 'addr4view',
          component: ViewInsure
        },
        {
          path: 'policy/edit',
          name: 'addr4put',
          component: EditPolicy
        },
        // 方案管理
        {
          path: 'case/list',
          name: 'caselist',
          component: CaseList
        },
        {
          path: 'case/add',
          name: 'case4add',
          component: AddCase
        },
        {
          path: 'case/edit',
          name: 'case4put',
          component: EditCase
        },
        {
          path: 'case/view',
          name: 'case4get',
          component: ViewCase
        },
        {
          path: 'case/editInsurance',
          name: 'caseitem4edit',
          component: EditInsurance
        },
        {
          path: 'case/change',
          name: 'caseitem4editbase',
          component: ChangeCase
        },
        // 社保补缴规则
        {
          path: 'case/payBackList',
          name: 'paybacklist',
          component: PayBackList
        },
        {
          path: 'case/addPayBack',
          name: 'addpayback',
          component: AddPayBack
        },
        {
          path: 'case/viewPayBack',
          name: 'payback4get',
          component: ViewPayBack
        },
        {
          path: 'case/editPayBack',
          name: 'payback4put',
          component: EditPayBack
        },
        // 公积金方案
        {
          path: 'case/fundList',
          name: 'fundcaselist',
          component: FundList
        },
        {
          path: 'case/addFund',
          name: 'accfundcase4add',
          component: AddFund
        },
        {
          path: 'case/viewFund',
          name: 'accfundcase4get',
          component: ViewFund
        },
        {
          path: 'case/editFund',
          name: 'accfundcase4edit',
          component: EditFund
        },
        {
          path: 'case/editFundInsurance',
          name: 'accfundcaseitem4edit',
          component: EditFundInsurance
        },
        {
          path: 'case/changeFund',
          name: 'accfundcasebase4put',
          component: ChangeFund
        },
        // 公积金补缴规则
        {
          path: 'case/fundPayBackList',
          name: 'fundpaybacklist',
          component: FundPayBackList
        },
        {
          path: 'case/addFundPayBack',
          name: 'fundpayback4add',
          component: AddFundPayBack
        },
        {
          path: 'case/viewFundPayBack',
          name: 'fundpayback4get',
          component: ViewFundPayBack
        },
        {
          path: 'case/editFundPayBack',
          name: 'fundpayback4put',
          component: EditFundPayBack
        },
        // 残保金
        {
          path: 'case/residualList',
          name: 'residuallist',
          component: ResidualList
        },
        {
          path: 'case/addResidual',
          name: 'residual4add',
          component: AddResidual
        },
        {
          path: 'case/editResidual',
          name: 'residual4put',
          component: EditResidual
        },
        // 服务公司
        {
          path: 'company/list',
          name: 'company4list',
          component: companyList
        },
        {
          path: 'company/add',
          name: 'company4add',
          component: AddCompany
        },
        {
          path: 'company/edit',
          name: 'company4edit',
          component: EditCompany
        },
        {
          path: 'company/view',
          name: 'company4get',
          component: ViewCompany
        },
        // 商保方案
        {
          path: 'business/list',
          name: 'business4list',
          component: BusinessList
        },
        {
          path: 'business/add',
          name: 'business4add',
          component: AddBusiness
        },
        {
          path: 'business/edit',
          name: 'business4edit',
          component: EditBusiness
        },
        {
          path: 'business/view',
          name: 'business4get',
          component: ViewBusiness
        },
        {
          path: 'business/addPrice',
          name: 'businessprice4edit',
          component: AddPrice
        },
        {
          path: 'business/viewPrice',
          name: 'businessprice4get',
          component: ViewPrice
        },
        // 员工管理
        {
          path: 'system/userList',
          name: 'user4list',
          component: UserList
        },
        {
          path: 'system/addUser',
          name: 'user4add',
          component: AddUser
        },
        {
          path: 'system/editUser',
          name: 'user4edit',
          component: EditUser
        },
        {
          path: 'system/viewUser',
          name: 'user4get',
          component: ViewUser
        },
        {
          path: 'system/userRole',
          name: 'user4role',
          component: UserRole
        },
        // 角色管理
        {
          path: 'system/roleList',
          name: 'role4list',
          component: RoleList
        },
        {
          path: 'system/editRole',
          name: 'role4edit',
          component: EditRole
        },
        //审批
        {
          path: 'system/checkList',
          name: 'check4list',
          component: CheckList
        },
        //核算
        //收款
        {
          path: 'collect/List',
          name: 'collection4list',
          component: CollectionList
        },
        {
          path: 'collect/View',
          name: 'collection4view',
          component: CollectionView
        },
        //申报中心
        {
          path: 'report/SoReport',
          name: 'report4so',
          component: ReportSo
        },
        {
          path: 'report/PeReport',
          name: 'report4pe',
          component: ReportPe
        },
        {
          path: 'report/AcReport',
          name: 'report4ac',
          component: ReportAc
        },
        {
          path: 'report/CoReport',
          name: 'report4co',
          component: ReportCo
        },
        {
          path: 'report/PaReport',
            name: 'report4pa',
          component: ReportPa
        },
        //实缴费用管理
        {
          path: 'manage/SoManage',
          name: 'manage4so',
          component: ManageSo
        },
        {
          path: 'manage/PeManage',
          name: 'manage4pe',
          component: ManagePe
        },
        {
          path: 'manage/AcManage',
          name: 'manage4ac',
          component: ManageAc
        },
        {
          path: 'manage/HsManage',
          name: 'manage4hs',
          component: ManageHs
        },
        //系统
        {
          path: 'xitong/manage',
          name: 'manage4',
          component: Manage
        },
        {
          path: 'xitong/hand',
          name: 'hand4',
          component: Hand
        },
      ]
    }
  ]
});
const noLoginUrls=[
    '/login'
    ,'/forget'
    ,'/noPermission'
    ,'/404'
    ,'/index'
]
router.beforeEach((to,from,next)=>{

    if(to.part===undefined){next();}
    if(noLoginUrls.indexOf(to.path)!=-1){
      next()
    }else if(localStorage.token &&checkRole(to) ){
      next()
    }else{
     router.push({name:'noPermission'})
     // next();
    }
    if(!localStorage.token ){
      router.push({name:'login'})
    }

  // next();
});

function checkRole(to) {
  let m = JSON.parse(localStorage.user).permissionList;
  let myMenu=new Array();
  m.forEach((e)=>{
    myMenu.push(e.code)
  })
  return myMenu.indexOf(to.name)!=-1
}
export default router;
